package com.sike.handler;

import com.alibaba.fastjson.JSON;

import com.sike.common.result.AuthResult;
import com.sike.common.result.Result;
import com.sike.common.result.ResultCode;
import com.sike.common.utils.WebUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;


import java.io.IOException;


@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException {
        AuthResult result = new AuthResult(ResultCode.FORBIDDEN.getCode(), "用户没有该资源的权限",null);
        String jsonString = JSON.toJSONString(result);
        WebUtils.renderString(response,jsonString);
    }
}
